package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.config.IniConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 设置开关量报警类型
 * Created by zwz on 2020/4/22.
 */
public class Cmd_27_0DFragment extends BaseFragment {

    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.sp_id)
    Spinner spId;


    public static Cmd_27_0DFragment newInstance() {
        Bundle args = new Bundle();
        Cmd_27_0DFragment fragment = new Cmd_27_0DFragment();
        fragment.setArguments(args);


        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_27_0d;
    }

    @Override
    protected void initView() {
        ArrayAdapter<String> mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, IniConfig.alarm_des);
        spType.setAdapter(mAdapter);
        String[] ids = {"1", "2", "3", "4"};
        mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, ids);
        spId.setAdapter(mAdapter);
    }




    private void set() {
        String type = String.format(Locale.CHINA, "%02d",
                Integer.parseInt(IniConfig.alarm_data.get(spType.getSelectedItemPosition())));

        String id = String.format(Locale.CHINA, "%02x", Integer.parseInt(spId.getSelectedItem().toString()));
sendData(CmdConfig.CMD_27_0D+" "+id+" "+type);


    }

    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_27, data);

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();

                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_27)
                        &&data[13].equals(CmdConfig.CMD_27_0D)) {
                    if (data[14].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }


    @OnClick(R.id.btn_set)
    public void onViewClicked() {
        set();
    }
}
